<template>
  <div class="content">
    <el-row class="bgW">
      <el-col :span="22">
        <el-tag type="info">新单统计规则：客户首次成交金额大于等于500以上且是新单的 系统判定为新单客户;<br/>
                            备注：新单金额不计算累计金额；
        </el-tag>
        <!--检索条件-->
        <el-form class="search" :inline="true" size="mini" :model="search">
          <el-form-item label="成交日期">
            <el-date-picker
              v-model="search.cj_time"
              type="date"
              class="w130"
              placeholder="开始日期"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          			<el-date-picker
          			  v-model="search.cj_endtime"
          			  type="date"
          			  class="w130"
          			  placeholder="结束日期"
          			  value-format="yyyy-MM-dd"
          			></el-date-picker>
          </el-form-item>
		  <el-form-item label="姓名">
            <el-input
              v-model="search.customer_name"
              maxlength="30"
              class="w100"
              placeholder="请输入"
              clearable
            />
          </el-form-item>
          <el-form-item label="手机号">
            <el-input
              v-model="search.telephone"
              maxlength="30"
              class="w100"
              placeholder="请输入"
              clearable
            />
          </el-form-item>

          <el-form-item label="门店/门诊" v-if="!userInfo.shop_id">
            <el-select
              v-model="search.shop_id"
              clearable
              remote
              filterable
              :remote-method="remoteMethod"
              placeholder="请输入"
              class="w120"
            >
              <el-option
                v-for="item in options.shop_id"
                :key="item.shopId"
                :label="item.shopName"
                :value="item.shopId"
              />
            </el-select>
          </el-form-item>

        </el-form>
      </el-col>
      <el-col :span="2" style="text-align: right;">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="getList('search')">搜索</el-button>
      </el-col>
    </el-row>
    <div class="accountCon">
      <div class="btnBox">
        <el-button

          type="primary"
          :disabled="!idList.length"
          class="mb10"
          size="mini"
          @click="hint('导出选中列表','')"
        >导出数据</el-button>
        <!-- <el-button type="primary" class="mb10" size="mini" @click="tapBtn('添加客户','')">添加客户</el-button> -->
      </div>
      <!--列表-->
      <el-row>
        <el-col :span="24">
          <el-table
            :data="list"
            size="small"
            border
            :cell-style="$style.cellStyle"
            :header-cell-style="$style.rowClass"
            @selection-change="handleSelectionChange"
          >
            <ElTableColumn  type="selection" width="50" />
            <ElTableColumn label="成交日期" width="140" prop="cj_time" />
            <ElTableColumn label="姓名" prop="customer_name" />
            <ElTableColumn label="电话" prop="telephone" />
            <ElTableColumn label="性别" prop="sex" />
            <ElTableColumn label="年龄" prop="age" />
            <ElTableColumn label="生日" prop="birthday" />
            <ElTableColumn label="来源" prop="source_name" />
            <ElTableColumn label="会员" prop="grade_name" />
            <ElTableColumn label="市场" prop="market_name" />
            <ElTableColumn label="店面" prop="shop_name" />
            <ElTableColumn label="护理师" prop="nurse_name" />
            <ElTableColumn label="成交金额" prop="cj_money" />
            <ElTableColumn label="修复进展" prop="progress_name" />
            <!-- <ElTableColumn label="会员等级" prop="grade_name" /> -->
            <ElTableColumn label="疤痕类型" width="100" prop="scar_aggregate" />

			<ElTableColumn label="操作" fixed="right">
			  <template slot-scope="{ row }">
			    <span class="theme" @click="tapBtn('客户详情',row)">查 看</span>
			  </template>
			</ElTableColumn>

          </el-table>
          <pagination
            class="page tc mt10"
            :total="page.total"
            :page.sync="page.page"
            :limit.sync="page.limit"
            @pagination="getList"
          />
        </el-col>
      </el-row>
    </div>

  </div>
</template>

<script>
import { mapGetters } from "vuex";
import pagination from "@/components/Pagination";
import { addReturnVistT } from "@/api/againVisit";
import {
  newOrderlist,
  ssettingList,
  getGradeList,
  clientExport
} from "@/api/client";
import { getShopList } from "@/api/shop";

class Search {
  customer_name = "";
  telephone = "";
  shop_id = "";
  cj_time = "";
  cj_endtime="";
}
class Options {

}
class Page {
  total = 0;
  page = 1;
  limit = 10;
}
class Detail {
  customer_id = "";
  shop_id = "";
  shop_name = "";
  nurse_id = "";
  result_id = "";
  result = 1;
  service_attitude = 0;
  operation_process = 0;
  effect = 0;
  hygiene = 0;
  total = "";
  early_warning = 0;
  early_warning_content = "";
  accountability_content = "";
  accountability = 0;
  setting_id = "";
  return_visit_content = "";
  remarks = "";
}
class Rules {
  setting_id = [
    { required: false, message: "请选择问题分类", trigger: "change" }
  ];
  nurse_id = [{ required: true, message: "请选择护理师", trigger: "change" }];
  result_id = [
    { required: true, message: "请选择投诉类型", trigger: "change" }
  ];

  total = [
    {
      pattern: /^(?:[1-9]?\d|100)$/,
      message: "请输入0~100数字",
      trigger: "change"
    },
    { required: false, message: "请输入客户评分", trigger: "blur" }
  ];

  effect_problem_id = [
    { required: false, message: "请选择效果问题", trigger: "blur" }
  ];
  service_issues_id = [
    { required: false, message: "请选择服务问题", trigger: "blur" }
  ];
  early_warning_content = [
    { required: true, message: "请选择回预警内容", trigger: "blur" }
  ];
  accountability_content = [
    { required: true, message: "请选择回追责内容", trigger: "blur" }
  ];
  return_visit_content = [
    { required: true, message: "请选择回访内容", trigger: "blur" }
  ];
}
export default {
  name: "ClientNew",
  components: {
    pagination
  },
  data() {
    return {
      search: new Search(),
      options: new Options(),
      page: new Page(),
      detail: new Detail(),
      rules: new Rules(),
      list: [],
      idList: "",
      visitClient: false
    };
  },
  filters: {
    statusFilter(val) {
      return ["启用", "禁用"][+val];
    }
  },
  computed: {
    ...mapGetters(["userInfo"])
  },
  mounted() {
    this.getOptions();
    this.getList();
  },
  methods: {
    // 获取选项
    getOptions() {
      ssettingList({ page: 1, limit: 100000 }).then(res => {
        let that = this;
        res.list.forEach(function(item) {
          if (item.category_id == 1) {
            that.options.scar_id.push(item);
          } else if (item.category_id == 4) {
            that.options.source_id.push(item);
          } else if (item.category_id == 5) {
            that.options.repair_progress.push(item);
          } else if (item.category_id == 8) {
            that.options.member_classification.push(item);
          } else if (item.category_id == 9) {
            that.options.setting_id.push(item);
          } else if (item.category_id == 11) {
            that.options.result_id.push(item);
          } else if (item.category_id == 12) {
            that.options.effect_problem_id.push(item);
          } else if (item.category_id == 13) {
            that.options.service_issues_id.push(item);
          }
        });
      });
      getGradeList().then(res => {
        this.options.membership_leve = res.list;
      });
    },
    // 获取列表
    getList(option) {
      if (option === "search") this.page = new Page();
      newOrderlist({ ...this.search, ...this.page, is_join_shop: 7 }).then(
        res => {
          this.list = res.list;
          this.page.total = res.dataCount;
        }
      );
    },
    // 搜索门店
    remoteMethod(e) {
      getShopList({ page: 1, limit: 100000, shopName: e }).then(res => {
        this.options.shop_id = res.list;
      });
    },

	// 跳转按钮
	tapBtn(routerName, row) {
	  //confirm(222);
	  const query = {
	    id: row.id,
	    activeName: "first",
	    customer_name: row.customer_name,
	    search: this.search,
	    pageName: "客户列表",
	    page:this.page
	  };
	  //this.$router.push({ name: routerName, query });
	  if(routerName=='客户信息'){
	    this.$router.push({ name: routerName, query });
	  }
	   if(routerName=='客户详情'){
	  let route = this.$router.resolve({ name: routerName, query });
	  window.open(route.href, '_blank');
	   }
	},

	// 提示
	hint(text) {
	  this.$confirm(`${text},是否确定？`, "提示", {
	    confirmButtonText: "确定",
	    cancelButtonText: "取消",
	    type: "warning"
	  })
	    .then(() => {
	      this.getExport();
	    })
	    .catch(() => {
	      this.$message({
	        type: "info",
	        message: "已取消"
	      });
	    });
	},

    // 导出
    getExport() {
      clientExport({ id: this.idList, type: 6 }).then(res => {
        window.open(res.data.url);
      });
    },
    // 确认提交
    confirm() {
      addReturnVistT({ ...this.detail }).then(res => {
        this.$message({
          type: "success",
          message: res.msg
        });
        this.visitClient = false;
        this.getList();
      });
    },
    // 改变表格选中状态
    handleSelectionChange(val) {
      this.idList = val.map(i => {
        return i.id;
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.mainColor {
  color: #2dc0a3;
}
.content {
  height: calc(100% - 56px);
  overflow-y: auto;
}
.bgW {
  background: #fff;
  border-radius: 3px;
  padding: 10px;
  margin-bottom: 20px;
}

.el-form-item {
  margin: 5px 20px 3px 0;
}
.accountCon {
  background: #fff;
  padding: 0px 10px 80px;
  .btnBox {
    display: flex;
    justify-content: space-between;
  }
}
.visit {
  display: flex;
  flex-wrap: wrap;
  .el-form-item {
    margin: 0px 20px 30px 0;
  }
}
.center {
  width: 100% !important;
}
.footerBtn {
  width: 100%;
  text-align: right;
}
</style>
